<?php 
	session_start();
	require("connect.php");
?>
<html><!-- InstanceBegin template="/Templates/temp02.dwt.php" codeOutsideHTMLIsLocked="false" -->
<style type="text/css">
<!--
#subcontent {
	position:absolute;
	left:243px;
	top:0px;
	width:706px;
	height:59px;
	z-index:1;
	background-color: #70a972;
}
#subcontent01 {
	position:absolute;
	left:22px;
	top:11px;
	width:287px;
	height:39px;
	font:Tahoma;
	font-size:30px;
	font-weight:600px;
	color:#E8E8E8;
	z-index:1;
}
#subcontent02 {
	position:absolute;
	left:312px;
	top:29px;
	font:Tahoma;
	font-size:18px;
	font-weight:500px;
	color:#E8E8E8;
	width:199px;
	height:25px;
	z-index:2;
}
-->
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Untitled Document</title>

<link rel="stylesheet" type="text/css" href="css/product.css" />

<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>

<body>
<?php 
function GetQuantity($id){
		$cart = $_SESSION["CART"];
		$quantity=0;
		foreach(array_keys($cart) as $value){
			if($value == $id){
				$quantity = $cart[$id];
				break;
			}
		}
		return $quantity;	
	} 
?>
<div id="wrap">
	<div id="title">
		<div id="title01">
			<span><a href="register.php">Register </a>|<a href="mycart.php"> My Cart </a>|<a href="login.php"> Login </a></span>
		</div>
	</div>
	<div id="header">
		<div id="searchform">
	    	<form action="Templates/search.php">
					<input type="text" name="s" value="" onKeyPress="this.style.backgroundColor='pink'" onBlur="this.style.backgroundColor='white'" onFocus="this.style.backgroundColor='pink'"/>
			</form>
		</div>
	</div>
	<div id="menu">
		<ul>
			<li><a href="index.php">TRANG CHỦ</a></li>
			<li><a href="introduce.php">GIỚI THIỆU</a></li>
			<li><a href="product_view.php">SẢN PHẨM</a></li>
			<li><a href="promotion.php">KHUYẾN MẠI</a></li>			
			<li><a href="tuvan.php">TƯ VẤN THIẾT BỊ</a></li>
			<li><a href="phanhoi.php">PHẢN HỒI</a></li>
		</ul>
   </div>
   <div id="submenu">
   		<div id="submennu01"><a href="#">Uy tín/Chất lượng</a></div>
  </div>
   <div id="center">
   		<div id="left">
			<div id="danhmuc"><a href="product_view.php?catid=0">DANH MỤC SẢN PHẨM</a></div>
			<div id="left01">
			<?php 
						$sql=mysql_query("select * from catagory");
						while($row=mysql_fetch_array($sql))
					{
					    echo "<font color=#F8F8F8>::</font>";
						echo "<a href='product_view.php?catid={$row["catID"]}'>{$row["catName"]}</a><br>";
						echo "<font color=#666633 >-------------------------------</font><br>";
					}
			?>
			</div>
			<div id="giohang">
				<div id="giohang01"><b style="font:Tahoma; font-size:14px; color:#FFFFFF;">GIỎ HÀNG CỦA BẠN</b></div>
			</div>
			<div id="cart">
			  <?php include("cartdetail.php");?></div>
		</div>
		<div id="subcontent">
			<div id="subcontent01">NỘI THẤT CAO CẤP</div>
			<div id="subcontent02">UY TÍN - CHẤT LƯỢNG</div>
		</div>
		<div id="content"><!-- InstanceBeginEditable name="vungsuaduoc" -->
		<?php
	$cart = $_SESSION["CART"];
	//var_dump($cart);
	$action = $_REQUEST["action"];
	//dung session nay de chong back
	
	switch($action){
		case "add":
				$id = $_REQUEST["pid"];	
				
				if($cart){
					if(array_key_exists($id,$cart))
					{
						$cart[$id] +=1;
					}else{
						$cart[$id]=1;
					}
				}else{
					$cart[$id]=1;
				}
				
				$_SESSION["CART"] = $cart;
				echo "<script>window.location='cart.php';</script>";	
			break;
		case "update":
		
			foreach(array_keys($cart) as $value){
				if($_REQUEST["quantity_".$value] >0)
				$cart[$value] = $_REQUEST["quantity_".$value];
			}
			$_SESSION["CART"] = $cart;
			//var_dump($_SESSION["CART"]);
			break;
		case "delete":
			$id = $_REQUEST["pid"];
			if($cart)
			{
				foreach(array_keys($cart) as $value){
					if($value != $id){
						$newcart[$value] = $cart[$value];
					}
				}
				$_SESSION["CART"] = $newcart;	
				$cart=$newcart;
			}
echo "<script>window.location='cart.php';</script>";			
			break;
	
	}
	
	

?>
<style type="text/css">
<!--
.style3 {font-family: tahoma; font-size: 12px; }
.style9 {font-size: 12}
.style10 {
	font-family: tahoma;
	font-weight: bold;
	font-size: 12px;
}
.style11 {font-size: 12px}
.style14 {font-family: tahoma}
.style16 {font-family: tahoma; font-weight: bold; }
-->
</style>
<script>
	a = true;
	function IsNumeric(txt,txt1)

	{
			sText = txt.value;
		   var ValidChars = "0123456789";
		   var IsNumber=true;
		   var Char;
		
		 
		   for (i = 0; i < sText.length && IsNumber == true; i++) 
			  { 
			  Char = sText.charAt(i); 
			  if (ValidChars.indexOf(Char) == -1) 
				 {
				 IsNumber = false;
				 a = false;
				 alert('Quantity is number');
				document.location="cart.php";
				 }
			  }
			 if (IsNumber==true)
			 	{
					m = parseInt(txt.value);
					n = parseInt(txt1.value);
			 	if (m>n) 
				{
					IsNumber=false;
					 alert('In store, we have only '+ n);
				document.location="cart.php";
				}
				}
		   return IsNumber;
   
   }
   
   function test(frm){
   		
		if(a){
			document.location="checkout.php";
		}
   }
</script>
<form id="form1" name="form1" method="post" action="cart.php" onSubmit="return a;">
<input type="hidden" id="action" name="action" value="update" />
  <center><table width="80%" border="0">
    <tr>
      <td width="40%"><div align="left"><strong><span class="style3">Product name</span></strong></div></td>
      <td width="14%"><div align="left"><strong><span class="style3">Quantity</span></strong></div></td>
      <td width="11%"><div align="left"><strong><span class="style3">Price</span></strong></div></td>
      <td width="12%"><div align="left"><span class="style10">Sub total</span></div></td>
      <td width="12%"><div align="left"><strong><span class="style3">Delete</span></strong></div></td>
    </tr>
    <tr>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
    </tr>
	<?php
		if($cart){
		$idlist = "";
		$total=0;
		foreach(array_keys($cart) as $value){
			$idlist = $idlist.$value.",";
		}
		$idlist = $idlist."0";
		$sql = "SELECT * FROM product WHERE pID IN (".$idlist.")";
		
		//require("connect.php");
		
		$result = mysql_query($sql);
		while($row = mysql_fetch_array($result)){
	?>
    <tr>
      <td><div align="left"><span class="style9"><?php echo $row["pName"]?></span></div></td>
      <td><div align="left"><span class="style9">
        </span><span class="style9">
          <label>
          </span>
        <span class="style9">
          <input name="quantity_<?php echo $row["pID"]?>" type="text" id="quantity_<?php echo $row["pID"]?>" value="<?php echo GetQuantity($row["pID"])?>" size="3" onBlur="return IsNumeric(this,form1.soluong_<?php echo $row["pID"] ?>)"/>
		  <input type="hidden" name="soluong_<?php echo $row["pID"];?>" value="<?php echo $row["pAmount"]?>">
            </span><span class="style9">
            </label>
          </span></div></td>
      <td><div align="left"><span class="style9"><?php echo number_format($row["pPrice"],2,'.',',');?></span></div></td>
      <td><div align="left"><span class="style11">
        <?php $total += $row["pPrice"]*GetQuantity($row["pID"]);echo number_format($row["pPrice"]*GetQuantity($row["pID"]),2,'.',',');?>
      </span></div></td>
      <td><div align="left"><span class="style9"><a href="cart.php?action=delete&pid=<?php echo $row["pID"]?>">Delete</a></span></div></td>
    </tr>
	<?php
		}
	
	}else{
	?>
	 <tr>
      <td><div align="left"><span class="style9">Cart is empty</span></div></td>
      <td><div align="left"><span class="style9"></span></div></td>
      <td><div align="left"><span class="style9"></span></div></td>
      <td><div align="left"><span class="style11"></span></div></td>
      <td><div align="left"><span class="style9"></span></div></td>
    </tr>
	<?php	
		}
	?>
    <tr>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
    </tr>
    <tr>
      <td><div align="left"><span class="style9"></span></div></td>
      <td><div align="left"><span class="style9"></span></div></td>
      <td><div align="center" class="style11">
        <div align="left"><span class="style16">Summary</span></div>
      </div></td>
      <td><div align="center" class="style11">
        <div align="left"><span class="style14"><?php echo number_format($total,2,'.',',');?></span></div>
      </div></td>
      <td><div align="left"><span class="style9">
      </span></div>        <span class="style9"><label></label>
      </span></td>
    </tr>
    <tr>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
    </tr>
    <tr>
      <td><div align="left"></div></td>
      <td><div align="left"></div></td>
      <td><div align="left"><span class="style9">
        <input type="submit" name="Submit" value="Update" />
      </span></div></td>
      <td><div align="left">
        <label>
        <input type="button" name="Submit2" value="Continue" onClick="window.location = 'product_view.php';"/>
        </label>
      </div></td>
      <td><div align="left">
        <label>
        <input type="button" name="Submit3" value="Thanh toán" onClick="test(this.form)"/>
        </label>
      </div></td>
    </tr>
  </table></center>
</form>
		<!-- InstanceEndEditable --></div>
   </div>
   <div id="footer01">
  		<center><a href="#">Design By H3TL&nbsp;Group3<br> Mọi thông tin chi tiết xin liên hệ: h3tl.itpro@gmail.com</a></center>
  </div>
   
</div>


</body>
<!-- InstanceEnd --></html>
